package com.wan37.logic.listener.handler;

import com.wan37.logic.domain.entity.UserWeaponryDO;
import com.wan37.logic.domain.vo.WeaponryVO;
import com.wan37.logic.global.reference.SpringContextHolder;
import com.wan37.logic.listener.annotation.EventHandler;
import com.wan37.logic.listener.annotation.Listener;
import com.wan37.logic.listener.event.EventType;
import com.wan37.logic.listener.event.user.UserLoginEvent;
import com.wan37.logic.service.UserWeaponryService;
import com.wan37.logic.service.cache.RoleServiceCache;
import com.wan37.logic.service.cache.UserServiceCache;
import com.wan37.logic.service.cache.WeaponryServiceCache;
import com.wan37.logic.utils.BuffUtil;
import com.wan37.logic.utils.RouteUtils;
import com.wan37.protobuf.constant.AllRoleLabel;
import com.wan37.protobuf.constant.BackpackLabel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : luoyong
 * @date : 2020-06-29 11:50
 **/
@Listener
public class WeaponryListenerHandler {
    private UserWeaponryService userWeaponryService;
    private WeaponryServiceCache weaponryServiceCache;
    private RoleServiceCache roleServiceCache;
    private UserServiceCache userServiceCache;
    private BuffUtil buffUtil;
    private RouteUtils routeUtils;

    public WeaponryListenerHandler() {
        this.userWeaponryService = SpringContextHolder.getBean("userWeaponryService");
        this.weaponryServiceCache = SpringContextHolder.getBean("weaponryServiceCache");
        this.roleServiceCache = SpringContextHolder.getBean("roleServiceCache");
        this.userServiceCache = SpringContextHolder.getBean("userServiceCache");
        this.buffUtil = SpringContextHolder.getBean("buffUtil");
        this.routeUtils = SpringContextHolder.getBean("routeUtils");
    }

    @EventHandler(value = EventType.LOGIN)
    public void login(UserLoginEvent userLoginEvent) {
        String userId = userLoginEvent.getUserId();
        List<UserWeaponryDO> userWeaponryDOList = userWeaponryService.getUserWeaponryDOsByUserId(userId);
        Map<String, WeaponryVO> weaponryVOMap = new HashMap<>();
        for (UserWeaponryDO userWeaponryDO : userWeaponryDOList) {
            WeaponryVO newWeaponryVO = new WeaponryVO(userWeaponryDO);
            WeaponryVO weaponryVO = weaponryServiceCache.getWeaponryServiceMap().
                    get(userWeaponryDO.getWeaponryId()).getWeaponryVO();
            newWeaponryVO.setLabel(BackpackLabel.WEAPONRY);
            newWeaponryVO.setName(weaponryVO.getName());
            int level = userWeaponryDO.getLevel();
            Map<String, Long> numberMap = new HashMap<>();
            weaponryVO.getNumberMap().keySet().forEach(s -> {
                numberMap.put(s, (long) level * weaponryVO.getNumberMap().get(s));
            });
            newWeaponryVO.setNumberMap(numberMap);
            newWeaponryVO.setMaxRepetition(weaponryVO.getMaxRepetition());
            newWeaponryVO.setProfession(weaponryVO.getProfession());
            newWeaponryVO.setPart(weaponryVO.getPart());
            /**
             * 初始化角色的装备，判断装备是否已经装备，进行角色数值更新
             */
            if (!newWeaponryVO.getUsage().equals(BackpackLabel.WEAPONRY_UNUSED)) {
                RoleServiceCache.Role role = roleServiceCache.getRoleServiceMap().get(userWeaponryDO.getUsage());
                role.getPartToWeaponry().put(newWeaponryVO.getPart(), newWeaponryVO.getSelfWeaponryId());
                Map<String, Long> roleNumberMap = role.getRoleVO().getNumberMap();
                /**
                 * 装备已经使用，需要进行角色数值的增加体现
                 */
                buffUtil.positive(newWeaponryVO.getNumberMap(), roleNumberMap);
            }
            weaponryVOMap.put(newWeaponryVO.getSelfWeaponryId(), newWeaponryVO);
        }
        userServiceCache.getUserServiceMap().get(userId).setWeaponryVOMap(weaponryVOMap);
    }
}
